package site.zhouinfo.imageUtils;

import java.awt.image.BufferedImage;

/**
 * 图片转换成Ascii图片
 *
 * @author zhou
 * @create 2017-03-29 11:01
 * @email zhouinfo@qq.com
 */
public class AsciiPic {

    static int compress = 80;

    public static void createAsciiPic(String path) {
        // 字符串由复杂到简单 字符样本
        String base = "@#%*o!;`.";
        //final BufferedImage image = ImageIO.read(new File(path));
        BufferedImage image = ImageUtil.compressImage(path, compress);
        int height = image.getHeight();
        int width = image.getWidth();
        // 字符行高=字符宽*2
        for (int y = 0; y < height; y += 2) {
            for (int x = 0; x < width; x++) {
                // 取像素的RGB颜色数值
                int pixel = image.getRGB(x, y);
                // 使用位移分别获取 R G B 数值
                int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;
                // RGB颜色转换成灰度
                float gray = 0.299f * r + 0.578f * g + 0.114f * b;
                // 由灰度计算对应的字符样本
                int index = Math.round(gray * (base.length() + 1) / 255);
                // 灰度太高就使用空格键代替
                System.out.print(index >= base.length() ? " " : String.valueOf(base.charAt(index)));
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        // 需要转换的图片地址
        //AsciiPic.createAsciiPic("C:\\Users\\zhoufox\\Pictures\\Camera Roll\\zhou.jpg");
        //System.out.println();
        //AsciiPic.createAsciiPic("C:\\Users\\zhoufox\\Pictures\\Camera Roll\\psu.jpg");
        //System.out.println();
        AsciiPic.createAsciiPic("C:\\Users\\zhoufox\\Pictures\\知乎素材\\侧脸.png");
        System.out.println();
        AsciiPic.createAsciiPic("C:\\Users\\zhoufox\\Pictures\\知乎素材\\夕阳.png");
        System.out.println();
        AsciiPic.createAsciiPic("C:\\Users\\zhoufox\\Pictures\\知乎素材\\玉玲.jpg");
        //System.out.println();
        //AsciiPic.createAsciiPic("C:\\Users\\zhoufox\\Pictures\\知乎素材\\树.png");
        //System.out.println();
        //AsciiPic.createAsciiPic("C:\\Users\\zhoufox\\Pictures\\知乎素材\\孤独.png");
    }
}
